﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;

namespace YouDaoNotesCodes
{
    class HttpProtocol
    {
        #region POST
        /// <summary>
        /// http-Post方式：一般应用于调用java接口或者C#接口
        /// </summary>
        /// <param name="ServerPage">调用服务地址</param>
        /// <param name="strXml">入参xml：xml格式参考soapUI请求报文</param>
        /// <returns></returns>
        private string HttpPost(string ServerPage, string strXml)
        {
            byte[] dataArray = Encoding.Default.GetBytes(strXml);
            //创建请求
            HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(ServerPage);
            request.Method = "POST";
            request.ContentLength = dataArray.Length;
            //request.ContentType = "application/x-www-form-urlencoded";//java webservice，举例，具体编码格式和ContentType参考soapUI请求时的方式
            //request.ContentType = "application/json;charset=UTF-8";//java rest，举例，具体编码格式和ContentType参考soapUI请求时的方式
            //request.ContentType = "text/xml; charset=utf-8" //C# webservice ，举例，具体编码格式和ContentType参考soapUI请求时的方式
            request.ContentType = "text/xml; charset=utf-8";
            //创建输入流
            Stream dataStream = null;
            try
            {
                dataStream = request.GetRequestStream();
            }
            catch (Exception)
            {
                return null;//连接服务器失败
            }
            //发送请求
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            //读取返回消息
            string res = string.Empty;
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                #region 这种方式读取到的是一个Xml格式的字符串
                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                res = reader.ReadToEnd();
                reader.Close();
                #endregion

                #region 这种方式读取到的是一个返回的结果字符串
                //Stream stream = response.GetResponseStream();        //获取响应流
                //XmlTextReader Reader = new XmlTextReader(stream);
                //Reader.MoveToContent();
                //result = Reader.ReadInnerXml(); 
                #endregion
            }
            catch (WebException ex)
            {
                HttpWebResponse response = (HttpWebResponse)ex.Response;
                StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8);
                res = sr.ReadToEnd();
                PubCommon.WriteLog("连接服务器失败" + res + ex.GetType() + ex.Message, System.Reflection.MethodBase.GetCurrentMethod().Name);
                return null;//连接服务器失败
            }
            return res;
        }
        #endregion

        #region GET
        public string HttpGet(string url, int Timeout)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Method = "GET";
            request.ContentType = "text/html;charset=UTF-8";
            request.UserAgent = null;
            request.Timeout = Timeout;

            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            Stream myResponseStream = response.GetResponseStream();
            StreamReader myStreamReader = new StreamReader(myResponseStream, Encoding.GetEncoding("utf-8"));
            string retString = myStreamReader.ReadToEnd();
            myStreamReader.Close();
            myResponseStream.Close();

            return retString;
        }
        #endregion
    }
}
